package com.ruoyi.commerce.service.impl;

import java.util.List;

import com.ruoyi.commerce.domain.CommerceInfoUser;
import com.ruoyi.commerce.service.ICommerceInfoUserService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.commerce.mapper.CommerceInfoMapper;
import com.ruoyi.commerce.domain.CommerceInfo;
import com.ruoyi.commerce.service.ICommerceInfoService;

/**
 * 帮养之家信息Service业务层处理
 * 
 * @author zhezhi
 * @date 2022-09-30
 */
@Service
public class CommerceInfoServiceImpl implements ICommerceInfoService 
{
    @Autowired
    private CommerceInfoMapper commerceInfoMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ICommerceInfoUserService infoUserService;


    /**
     * 查询帮养之家信息
     * 
     * @param infoId 帮养之家信息主键
     * @return 帮养之家信息
     */
    @Override
    public CommerceInfo selectCommerceInfoByInfoId(Long infoId)
    {
        return commerceInfoMapper.selectCommerceInfoByInfoId(infoId);
    }

    /**
     * 查询帮养之家信息列表
     * 
     * @param commerceInfo 帮养之家信息
     * @return 帮养之家信息
     */
    @Override
    public List<CommerceInfo> selectCommerceInfoList(CommerceInfo commerceInfo)
    {
        return commerceInfoMapper.selectCommerceInfoList(commerceInfo);
    }

    /**
     * 新增帮养之家信息
     * 
     * @param commerceInfo 帮养之家信息
     * @return 结果
     */
    @Override
    public int insertCommerceInfo(CommerceInfo commerceInfo) throws Exception {
        commerceInfo.setCreateTime(DateUtils.getNowDate());
        int i = commerceInfoMapper.insertCommerceInfo(commerceInfo);
        CommerceInfo commerceInfo1 = commerceInfoMapper.selectByEnd();
        String leader = commerceInfo.getLeader();
        SysUser sysUser = userService.selectUserByUserName(leader);
        CommerceInfoUser commerceInfoUser = new CommerceInfoUser();
        commerceInfoUser.setInfoId(commerceInfo1.getInfoId());
        commerceInfoUser.setUserId(sysUser.getUserId());
        int flag = infoUserService.insertCommerceInfoUser(commerceInfoUser);
        if(flag==0){
            throw new Exception("关联失败");
        }
        return i;
    }

    /**
     * 修改帮养之家信息
     * 
     * @param commerceInfo 帮养之家信息
     * @return 结果
     */
    @Override
    public int updateCommerceInfo(CommerceInfo commerceInfo)
    {
        CommerceInfo commerceInfo1 = commerceInfoMapper.selectCommerceInfoByInfoId(commerceInfo.getInfoId());
        boolean flag = commerceInfo1.getLeader().equals(commerceInfo.getLeader());
        if(!flag){
            SysUser sysUser = userService.selectUserByUserName(commerceInfo.getLeader());
            CommerceInfoUser commerceInfoUser = new CommerceInfoUser();
            commerceInfoUser.setUserId(sysUser.getUserId());
            commerceInfoUser.setInfoId(commerceInfo.getInfoId());
            commerceInfoUser.setUpdateTime(DateUtils.getNowDate());
            infoUserService.updateCommerceInfoUser(commerceInfoUser);
        }

        commerceInfo.setUpdateTime(DateUtils.getNowDate());
        return commerceInfoMapper.updateCommerceInfo(commerceInfo);
    }

    /**
     * 批量删除帮养之家信息
     * 
     * @param infoIds 需要删除的帮养之家信息主键
     * @return 结果
     */
    @Override
    public int deleteCommerceInfoByInfoIds(Long[] infoIds)
    {

        infoUserService.deleteCommerceInfoUserByInfoIds(infoIds);

        return commerceInfoMapper.deleteCommerceInfoByInfoIds(infoIds);
    }

    /**
     * 删除帮养之家信息信息
     * 
     * @param infoId 帮养之家信息主键
     * @return 结果
     */
    @Override
    public int deleteCommerceInfoByInfoId(Long infoId)
    {
        return commerceInfoMapper.deleteCommerceInfoByInfoId(infoId);
    }
}
